package com.ruoyi.baseData.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.baseData.domain.Shift;
import com.ruoyi.baseData.domain.Team;
import com.ruoyi.baseData.domain.TeamUser;
import com.ruoyi.baseData.domain.dto.TeamDto;
import com.ruoyi.baseData.domain.vo.TeamUserVo;
import com.ruoyi.baseData.domain.vo.TeamVo;
import com.ruoyi.baseData.mapper.TeamMapper;
import com.ruoyi.baseData.mapper.TeamUserMapper;
import com.ruoyi.baseData.service.TeamService;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class TeamServiceImpl implements TeamService {
    @Resource
    TeamMapper teamMapper;

    @Resource
    TeamUserMapper teamUserMapper;




    @Override
    public Page<TeamVo> getTeamByPage(TeamDto pageEntity) {
        MPJLambdaWrapper<Team> wrapper = new MPJLambdaWrapper<Team>()
                .selectAll(Team.class)//查询user表全部字段
                //记的测试阶段
                .eq(Team::getDelFlag, 0)
                .orderByDesc(Team::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, Team.class, pageEntity);
        Page<TeamVo> userList = teamMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   TeamVo.class, wrapper);
        return userList;
    }

    @Override
    public void addTeam(TeamDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<Team> wrapper = new MPJLambdaWrapper<Team>()
                .eq(Team::getCode, testDto.getCode())
                .ne(Team::getDelFlag, 1);
        Team material = teamMapper.selectOne(wrapper);
        if(null != material) {
            throw new GlobalException("该material已经存在");
        }
        Team test2 = new Team();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
        test2.setDelFlag(false);
        test2.setCreateBy(userName);
        test2.setCreateTime(new Date());
        teamMapper.insert(test2);
    }

    @Override
    public void updateTeam(TeamDto testDto) {
        MPJLambdaWrapper<Team> wrapper = new MPJLambdaWrapper<Team>()
                .eq(Team::getCode, testDto.getCode());
        Team material = teamMapper.selectOne(wrapper);
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        BeanUtils.copyProperties(testDto, material);
        material.setCreateBy("admin");
        material.setCreateTime(new Date());
        teamMapper.updateById(material);
    }

    @Override
    public void delTeam(TeamDto testDto) {
        Team material = teamMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        material.setDelFlag(true);
        teamMapper.updateById(material);
    }

    @Override
    public List<TeamUserVo> getUserByTeamId(TeamDto teamDto) {

        MPJLambdaWrapper<TeamUser> wrapper = new MPJLambdaWrapper<TeamUser>()
                .eq(TeamUser::getTeamId, teamDto.getId())
                .eq(TeamUser::getDelFlag, 0);

        List<TeamUserVo> teamUserVos = teamUserMapper.selectJoinList(TeamUserVo.class, wrapper);
        return teamUserVos;
    }

    @Override
    public void addTeamUser(TeamDto teamDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<TeamUser> wrapper = new MPJLambdaWrapper<TeamUser>()
                .eq(TeamUser::getTeamId, teamDto.getId())
                .eq(TeamUser::getDelFlag, 0);

        if(null != teamDto.getUserIds() && teamDto.getUserIds().size() > 0) {
            //先删除
//            List<TeamUser> teamUserVos = teamUserMapper.selectJoinList(TeamUser.class, wrapper);
            teamUserMapper.delete(wrapper);
            for(Integer i : teamDto.getUserIds()) {
               TeamUser teamUser  = new TeamUser();
                teamUser.setTeamId(teamDto.getId());
                teamUser.setUserId(i);
                teamUser.setCreateBy(userName);
                teamUser.setCreateTime(new Date());
                teamUser.setDelFlag(false);
                teamUserMapper.insert(teamUser);
            }
        }
    }

    @Override
    public String uploadTeam(MultipartFile file) throws IOException {
        return null;
    }
}




